package com.cadre.web.controller.officer;

import com.alibaba.fastjson.JSON;
import com.cadre.common.utils.R;
import com.cadre.common.utils.annotation.SysLog;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A26;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A01Service;
import com.cadre.system.service.A26Service;
import com.cadre.system.service.DIC_ZBService;
import com.cadre.web.controller.tool.A26VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"奖励接口"})
@RequestMapping({"/profess/a26"})
@RestController
/* loaded from: A26Controller.class */
public class A26Controller {
    @Resource
    private A26Service a26Service;
    @Resource
    private A01Service a01Service;
    @Resource
    private DIC_ZBService dic_zbService;
    @Autowired
    RestHighLevelClient client;

    @GetMapping({"/info/{flowId}"})
    public R info(@PathVariable("flowId") String flowId, Short flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("flowid", flowId);
        map.put("flag", flag);
        return R.ok().put("a26", (Object) this.a26Service.selectByList(map).get(0));
    }

    @PostMapping({"/upOrder"})
    @ApiOperation("保存奖励惩戒排序")
    public R upOrder(@RequestBody String[] flowids) {
        if (flowids.length == 0) {
            return R.error();
        }
        for (short i = 0; i < flowids.length; i = (short) (i + 1)) {
            A26 a26 = new A26();
            a26.setFlowid(flowids[i]);
            a26.setSerial(Short.valueOf(i));
            this.a26Service.updateByPrimaryKeySelective(a26);
        }
        return R.ok();
    }

    @GetMapping({"/list"})
    @ApiOperation("获取奖惩信息")
    public R list(String personid, Short flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        map.put("flag", flag);
        return R.ok().put("data", (Object) this.a26Service.selectByList(map));
    }

    @PostMapping({"/save"})
    @SysLog(value = "编辑奖惩信息", target = "A01", set = "A26", type = 0)
    @ApiOperation("编辑奖惩信息")
    public R save(@RequestBody A26VO a26VO) {
        try{
            String personid = a26VO.getPersonid();
            short flag = a26VO.getFlag();
            String a2601 = a26VO.getA2601();
            List<A26> a26list = a26VO.getA26list();
            this.a26Service.deleteByPersonIdAndFlag(personid, flag);
            if (a26list.size() > 0) {
                for (A26 a26 : a26list) {
                    a26.setPersonid(personid);
                    a26.setFlowid(GenerateUUID.getUUID());
                    a26.setIsout((short) 1);
                    this.a26Service.insertSelective(a26);
                }
            }
            Map<String, Object> maps = new HashMap<>();
            maps.put("personid", personid);
            List<A26> a26s = this.a26Service.selectByList(maps);
            A01 a01 = this.a01Service.selectByPrimaryKey(personid);
            if (a01 != null) {
                if (flag == 1) {
                    a01.setA2601(a2601);
                } else if (flag == 2) {
                    a01.setA2701(a2601);
                }
                this.a01Service.updateByPrimaryKeySelective(a01);
            }
            Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, personid), RequestOptions.DEFAULT).getSource();
            if (source != null) {
                A01 a01s = ESUtil.useObjectMapper(source);
                a01s.setA26List(a26s);
                this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
            }
        }catch (Exception e) {
            e.printStackTrace();;
        }

        return R.ok();
    }

    @PostMapping({"/saveA2601"})
    public R saveA01(@RequestBody A26[] a26) {
        String att = "";
        if (a26.length <= 0) {
            return R.error();
        }
        for (int i = 0; i < a26.length; i++) {
            this.a26Service.updateByPrimaryKeySelective(a26[i]);
            att = att + a26[i].getA2625() + "经" + a26[i].getA2615() + "批准," + a26[i].getA2605() + ";";
        }
        String att2 = att.substring(0, att.length() - 1);
        a26[0].getFlag();
        if (a26[0].getFlag().shortValue() == 1) {
            A01 a01 = new A01();
            a01.setA2601(att2);
            a01.setFlowid(a26[0].getPersonid());
            if (this.a01Service.updateByPrimaryKeySelective(a01) == 1) {
                return R.ok().put("data", (Object) att2);
            }
            return R.error();
        } else if (a26[0].getFlag().shortValue() != 2) {
            return R.error();
        } else {
            A01 a012 = new A01();
            a012.setA2701(att2);
            a012.setFlowid(a26[0].getPersonid());
            if (this.a01Service.updateByPrimaryKeySelective(a012) == 1) {
                return R.ok().put("data", (Object) att2);
            }
            return R.error();
        }
    }

    @PostMapping({"/update"})
    @SysLog(value = "修改奖惩信息", target = "A01", set = "A26", type = 1)
    @ApiOperation("改")
    public R update(@RequestBody A26 a26) throws IOException {
        if (a26 != null) {
            String a2601 = a26.getA2601();
            String a2610 = a26.getA2610();
            if (!(a2601 == null || a2601 == "")) {
                String att = a2601.substring(a2601.length() - 1, a2601.length());
                if ("；".equals(att) || "。".equals(att) || "、".equals(att) || "，".equals(att) || ",".equals(att) || ".".equals(att) || "?".equals(att) || ";".equals(att)) {
                    a2601 = a2601.substring(0, a2601.length() - 1);
                }
            }
            if (!(a2610 == null || a2610 == "")) {
                String att2 = a2610.substring(a2610.length() - 1, a2610.length());
                if ("；".equals(att2) || "。".equals(att2) || "、".equals(att2) || "，".equals(att2) || ",".equals(att2) || ".".equals(att2) || "?".equals(att2) || ";".equals(att2)) {
                    a2610 = a2610.substring(0, a2610.length() - 1);
                }
            }
            a26.setA2601(a2601);
            a26.setA2610(a2610);
        }
        this.a26Service.updateByPrimaryKeySelective(a26);
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, a26.getFlowid()), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            Map<String, Object> map = new HashMap<>();
            map.put("personid", a26.getFlowid());
            a01s.setA26List(this.a26Service.selectByList(map));
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }

    @PostMapping({"/delete"})
    @SysLog(value = "删除奖惩信息", target = "A01", set = "A26", type = DateUtil.SUB_MONTH)
    @ApiOperation("删")
    public R delete(@RequestBody String[] flowids) {
        this.a26Service.getBaseMapper().deleteBatchIds(Arrays.asList(flowids));
        return R.ok();
    }

    @GetMapping({"/chengjie/mingcheng/{rootId}"})
    @ApiOperation("惩戒类别")
    public R chenghjieMingcheng(@PathVariable("rootId") String rootId) {
        return R.ok().put("mingcheng", (Object) this.dic_zbService.selectChengjieMingcheng(rootId));
    }
}
